package com.arvato.right.service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.right.bean.bean_do.WxworkRightUserDo;
import com.arvato.right.bean.bean_do.WxworkRightUserRoleDo;
import com.arvato.right.bean.bean_vo.WxworkRightResourceVo;
import com.arvato.right.bean.bean_vo.WxworkRightRoleVo;
import com.arvato.right.bean.bean_vo.WxworkRightUserVo;
import com.arvato.right.mapper.WxworkRightRoleMapper;
import com.arvato.right.mapper.WxworkRightUserMapper;
import com.arvato.right.mapper.WxworkRightUserRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
@Transactional
public class WxworkRightUserService {
	@Autowired
	private WxworkRightUserMapper wxworkRightUserMapper;
	
	@Autowired
	private WxworkRightResourceService wxworkRightResourceService;
	
	@Autowired
	private WxworkRightUserRoleMapper wxworkRightUserRoleMapper;

	@Autowired
	private WxworkRightRoleMapper wxworkRightRoleMapper;
	
	/**
	 * 根据条件查询多个用户(分页)
	 * @param query
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageData listUserByQuery(Query query, int pageNum, int pageSize) {
		// 设置分页参数
		PageHelper.startPage(pageNum, pageSize);
		// 执行查询
		List<WxworkRightUserVo> userList = wxworkRightUserMapper.listUserByQuery(query);
		// 得到结果
		PageInfo<WxworkRightUserVo> pageInfo = new PageInfo<WxworkRightUserVo>(userList);
		List<WxworkRightUserVo> list = pageInfo.getList();
		for(WxworkRightUserVo userVo : list) {
			List<WxworkRightRoleVo> roleList = wxworkRightRoleMapper.listRoleByUserId(userVo.getUserId());
			userVo.setRoleList(roleList);
		}
		PageData data = new PageData(pageInfo.getTotal(), list);
		return data;
	}
	/**
	 * 根据条件查询多个用户(不分页)
	 * @param query
	 * @return
	 */
	public List<WxworkRightUserVo> listUserByQuery(Query query) {
		List<WxworkRightUserVo> userList = wxworkRightUserMapper.listUserByQuery(query);
		return userList;
	}
	
	/**
	 * 根据条件 查询一个用户
	 * @param query
	 * @return
	 */
	public WxworkRightUserVo getUserByQuery(Query query) {
		List<WxworkRightUserVo> userList = wxworkRightUserMapper.listUserByQuery(query);
		// 在查这个角色的资源
		if(userList.size() >= 1){
			WxworkRightUserVo user = userList.get(0);
			List<WxworkRightResourceVo> resourceList = wxworkRightResourceService.listResourceByUserId(user.getUserId());
			user.setResourceList(resourceList);
			System.out.println("资源数量:" + resourceList.size());
			return user;
		}
		return null;
	}
	
	/**
	 * 新增一个用户
	 * @param user
	 * @param roleIds 
	 * @return
	 */
	public int insert(WxworkRightUserDo user, String[] roleIds) {
		int row = wxworkRightUserMapper.insert(user);
		if(row == 1 && roleIds != null){
			// 然后给用户分配角色
			for (String roleId : roleIds) {
				WxworkRightUserRoleDo userRoleDo = new WxworkRightUserRoleDo();
				userRoleDo.setRoleId(roleId);
				userRoleDo.setUserId(user.getUserId());
				userRoleDo.setCreateDate(new Date());
				wxworkRightUserRoleMapper.insert(userRoleDo);
			}
		}
		return row;
	}
	
	/**
	 * 编辑用户
	 * @param user
	 * @param roleIds 
	 * @return
	 */
	public int update(WxworkRightUserDo user, String[] roleIds) {
		int row = wxworkRightUserMapper.update(user);
		Query query = new Query();
		query.put("userId", user.getUserId());
		if(row == 1){
			// 删除此用户的所有角色
			wxworkRightUserRoleMapper.deleteByQuery(query);
			// 重新给此用户分配新的角色
			WxworkRightUserRoleDo userRoleDo = new WxworkRightUserRoleDo();
			if(roleIds != null) {
				for (String roleId : roleIds) {
					userRoleDo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
					userRoleDo.setRoleId(roleId);
					userRoleDo.setCreateDate(new Date());
					userRoleDo.setUserId(user.getUserId());
					wxworkRightUserRoleMapper.insert(userRoleDo);
				}	
			}
		}
		return row;
	}
	
	/**
	 * 编辑用户 不编辑角色
	 * @param user
	 * @return
	 */
	public int update(WxworkRightUserDo user) {
		int row = wxworkRightUserMapper.update(user);
		return row;
	}
	
	/**
	 * 删除用户
	 * @param userId
	 * @return
	 */
	public int delete(String userId) {
		int row = wxworkRightUserMapper.delete(userId);
		return row;
	}
	
	/**
	 * 根据ID查询用户
	 * @param valueOf
	 * @return
	 */
	public WxworkRightUserVo getUserById(String userId) {
		WxworkRightUserVo user = wxworkRightUserMapper.getUserById(userId);
		if(user != null){
			List<WxworkRightResourceVo> resourceList = wxworkRightResourceService.listResourceByUserId(user.getUserId());
			user.setResourceList(resourceList);
			System.out.println("查询角色:" + resourceList.size());
		}
		return user;
	}
	
	/**
	 * 比较纯粹的查询一个部门下直属有多少人
	 * 这些人不包过这个部门的子部门下的人
	 * @param query
	 * @return
	 */
	public List<WxworkRightUserVo> listUserByDepartmentId(Query query) {
		return wxworkRightUserMapper.listUserByQuery(query);
	}
	
}
